<script setup>
import {onMounted, ref} from "vue";
import {listPermission} from "../../../http/manage/permission.js";

let permissionTableData = ref([
  {
    name: "创建账户",
    url: "/api/manage/account",
    type: "POST",
    value: "manage:account:create",
    status: "enable",
    sort: 0,
  },
  {
    name: "删除账户",
    url: "/api/manage/account",
    type: "DELETE",
    value: "manage:account:delete",
    status: "enable",
    sort: 0,
  },
  {
    name: "修改账户",
    url: "/api/manage/account",
    type: "PUT",
    value: "manage:account:update",
    status: "enable",
    sort: 0,
  },
  {
    name: "查询账户",
    url: "/api/manage/account",
    type: "GET",
    value: "manage:account:select",
    status: "enable",
    sort: 0,
  },
])
let permissionGroupTreeData = ref([
  {
    name: "系统管理",
    children: [
      {
        name: "账户管理",
      },
      {
        name: "机构管理",
      },
      {
        name: "角色管理",
      },
      {
        name: "视图管理",
      },
      {
        name: "权限管理",
      },
      {
        name: "字典管理",
      },
    ],
  },
])

const defaultProps = {
  children: 'children',
  label: 'name',
}


onMounted(() => {
  handlePermissionList()
})

function handlePermissionList(name) {
  listPermission(name).then(result => {
    permissionTableData.value = result.data
  }).catch(error => {
    console.log(error)
  })
}
</script>

<template>
  <div style="display:flex; width: 350px; flex-flow: column nowrap; height: 100%; overflow: hidden;">
    <div style="display: flex; flex-flow: row nowrap; align-items: center; height: 40px;">
      <el-button type="primary" @click="handleDictCreate({})">New</el-button>
    </div>
    <el-tree
        default-expand-all
        :data="permissionGroupTreeData"
        :props="defaultProps">
      <template #default="{ node, data }">
        <span style="flex: 1; display: flex; justify-content: space-between; align-items: center; width: 200px">
          <span>{{ node.label }}</span>
          <span>
            <a @click="append(data)"> Append </a>
            <a style="margin-left: 8px" @click="remove(node, data)"> Delete </a>
          </span>
        </span>
      </template>
    </el-tree>
  </div>
  <div style="display: flex; flex-flow: column nowrap; width: 100%; height: 100%; overflow: hidden;">
    <div style="display: flex; flex-flow: row nowrap; align-items: center; height: 40px;">
      <el-form :inline="true" style="height: 30px">
        <el-form-item label="Name">
          <el-input clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleDictList">Search</el-button>
          <el-button type="primary" @click="handleDictCreate({})">New</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="display: flex; width: 100%; height: 100%; overflow: auto;">
      <el-table
          highlight-current-row
          @row-click="handleDictTableRowClick"
          :data="permissionTableData" border style="width: 100%;">
        <el-table-column prop="name" label="name" min-width="200"/>
        <el-table-column prop="url" label="url" min-width="200"/>
        <el-table-column prop="type" label="type" min-width="50"/>
        <el-table-column prop="value" label="value" min-width="200"/>
        <el-table-column prop="status" label="status" width="70">
          <template #default="scope">
            <el-switch
                active-value="enable"
                inactive-value="disable"
                v-model="scope.row.status "
            />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="sort" width="60"/>
        <el-table-column label="operations" width="180">
          <template #default="scope">
            <el-button @click="handleEdit(scope.$index, scope.row)">
              Edit
            </el-button>
            <el-popconfirm
                width="170"
                hide-icon
                confirm-button-text="确定"
                cancel-button-text="取消"
                @confirm="handleDictDelete(scope.row)"
                :title="'是否确认删除权限数据 [ ' + scope.row.name + ' ] ？'">
              <template #reference>
                <el-button type="danger">
                  Delete
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="flex: none; display: flex; flex-flow: row nowrap; align-items: center; height: 36px; border-top: #5cadff 1px solid;">
      <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="1000"/>
    </div>
  </div>
</template>
<style scoped>

</style>